<?php

require "include/bittorrent.php";
dbconn();
loggedinorreturn();
$TBDEVbot_id = $betsrobotid; //帖子发布者ID
$forumid = $betsforumid; //论坛版块ID
$HTMLOUT = "";
$subject = "";
if ($CURUSER["class"] < UC_MODERATOR && $CURUSER['id'] != $betsadmin)
	stderr("错误", "权限不足");

//==Autopost By Retro
function auto_bet($subject = "Error - Subject Missing", $body = "Error - No Body") {
	global $TBDEVbot_id, $forumid;
	$ress = sql_query("SELECT id FROM topics WHERE forumid = $forumid AND subject = $subject");
	if (mysql_num_rows($ress) == 1) {
		$arr = mysql_fetch_array($ress);
		$topicid = $arr['id'];
	} else {
		//$subject = sqlesc($subject . "结果");
		sql_query("INSERT INTO topics (userid, forumid, subject) VALUES ($TBDEVbot_id, $forumid, $subject)") or sqlerr(__FILE__, __LINE__);
		$topicid = mysql_insert_id();
	}
	$added = date("Y-m-d H:i:s", TIMENOW);
	sql_query("INSERT INTO posts (topicid, userid, added, body) " . "VALUES ($topicid, $TBDEVbot_id, '$added', $body)") or sqlerr(__FILE__, __LINE__);
	$res = sql_query("SELECT id FROM posts WHERE topicid = $topicid ORDER BY id DESC LIMIT 1") or sqlerr(__FILE__, __LINE__);
	$arr = mysql_fetch_row($res) or die("没有数据提交");
	$postid = $arr[0];
	sql_query("UPDATE topics SET lastpost = $postid, firstpost = $postid WHERE id = $topicid") or sqlerr(__FILE__, __LINE__);
}

//==End
$res1 = mysql_fetch_array(sql_query("SELECT id FROM topics ORDER BY id DESC LIMIT 1"));
$forumlink = $res1['id'] + 1;
$date = date("Y-m-d H:i:s", TIMENOW);
$id = isset($_GET['id']) && is_valid_id($_GET['id']) ? $_GET['id'] : 0;
$a = sql_query("SELECT * FROM betoptions WHERE id = " . sqlesc($id) . "") or sqlerr(__FILE__, __LINE__);
$b = mysql_fetch_array($a);
$gameid = $b['gameid'];
if ($gameid < 1) {
	header("location: bet_gamefinish.php");
	exit;
}
$c = sql_query("SELECT * FROM bets WHERE optionid =" . sqlesc($id) . "") or sqlerr(__FILE__, __LINE__);
$res3 = sql_query("SELECT * FROM betgames WHERE id = " . sqlesc($gameid) . " AND fix = 0") or sqlerr(__FILE__, __LINE__);
$o = @mysql_fetch_array($res3);
$s = mysql_fetch_array(sql_query("SELECT COUNT(*) FROM bets WHERE gameid = " . sqlesc($gameid) . "")) or sqlerr(__FILE__, __LINE__);
$totalstats = 0;
if (@mysql_num_rows($res3) == 1) {
	sql_query("UPDATE betgames SET fix = 1 WHERE id = " . sqlesc($gameid) . "") or sqlerr(__FILE__, __LINE__);
} else
	stderr('错误', '错误');
while ($d = mysql_fetch_array($c)) {
	$dividend = round(($d['bonus'] * $b['odds']) * 0.95, 0);
	if (mysql_num_rows(sql_query("SELECT * FROM bettop WHERE userid =" . sqlesc($d["userid"]) . "")) == 0) {
		sql_query("INSERT INTO bettop (userid, bonus, winbonus, winnum) VALUES (" . sqlesc($d["userid"]) . ", " . sqlesc($dividend - $d["bonus"]) . ", " . sqlesc($dividend - $d["bonus"]) . ",1)") or sqlerr(__FILE__, __LINE__);
	} else {
		sql_query("UPDATE bettop SET winnum = winnum + 1, bonus = bonus + " . sqlesc($dividend - $d["bonus"]) . ", winbonus = winbonus + " . sqlesc($dividend - $d["bonus"]) . " WHERE userid =" . sqlesc($d["userid"]) . "") or sqlerr(__FILE__, __LINE__);
	}
	$totalstats += $d['bonus'];
	$dividend = round(($d['bonus'] * $b['odds']) * 0.95, 0);
	$subjectwin = "博彩赢盘";
	$msg = "魔力值奖励 +" . $dividend;
	$msg2 = <<<EOD
本次盘面你获得了 {$dividend} 魔力值！
你在 [i]{$o['heading']}[/i] 盘面上将 {$d['bonus']} 魔力值押注在 [i]{$b['text']}[/i] 选项上，赔率为 {$b['odds']} ！

论坛相关帖子：[b][url=forums.php?action=viewtopic&forumid={$forumid}&topicid={$forumlink}]点击这里[/url][/b]
EOD;
	sql_query("UPDATE users SET seedbonus = seedbonus + " . sqlesc($dividend) . " WHERE id = " . sqlesc($d["userid"]) . "") or sqlerr(__FILE__, __LINE__);
	sql_query("INSERT INTO betlog(userid,msg,date,bonus) VALUES(" . sqlesc($d["userid"]) . ", " . sqlesc($msg) . ", '$date', " . sqlesc($dividend) . ")") or sqlerr(__FILE__, __LINE__);
	sql_query("INSERT INTO messages (sender, receiver, msg, added, subject) VALUES (0, $d[userid], " . sqlesc($msg2) . ", '$date', " . sqlesc($subjectwin) . ")") or sqlerr(__FILE__, __LINE__);
	$totalstats += $dividend;
}
$body = "[b]" . htmlspecialchars($o['heading']) . "[/b] - [i]" . htmlspecialchars($o['undertext']) . "[/i]\n\n";
$body.= "下注总数 : [b] " . htmlspecialchars($s[0]) . " [/b]\n";
$body.= "参与本次盘面魔力值 : [b] " . htmlspecialchars($totalstats) . "[/b]\n";
$body.= "获胜选项 : [b] " . htmlspecialchars($b['text']) . " [/b]\n";
$body.= "结盘操手 : [b] " . ($CURUSER['class'] != UC_STAFFLEADER ? htmlspecialchars($CURUSER['username']) : get_username(11, false, false, false, false, false, false, "", false, true)) . " [/b]\n";
$body.= "结盘时间 : [b] " . htmlspecialchars(date("Y-m-d H:i:s")) . " [/b]\n\n";
$body.= "[b]选项与倍率 :[/b]\n";
$res = sql_query("SELECT * FROM betgames WHERE id = " . sqlesc($gameid) . "") or sqlerr(__FILE__, __LINE__);
$a = mysql_fetch_array($res);
if ($a['sort'] == 0)
	$sort = "odds ASC";
elseif ($a['sort'] == 1)
	$sort = "id ASC";
$res2 = sql_query("SELECT * FROM betoptions WHERE gameid =" . sqlesc($a["id"]) . " ORDER BY $sort") or sqlerr(__FILE__, __LINE__);
while ($b = mysql_fetch_array($res2)) {
	$body.= " " . htmlspecialchars($b['text']) . " * [b]" . htmlspecialchars($b['odds']) . "[/b]\n";
}
$m = sql_query("SELECT users.username, users.id, bets.userid, bets.bonus FROM bets INNER JOIN users ON bets.userid = users.id WHERE optionid = " . sqlesc($id) . " AND gameid = " . sqlesc($gameid) . " ORDER BY bonus DESC LIMIT 20") or sqlerr(__FILE__, __LINE__);
$body.= "\n[b]Top 20 赌神：[/b]\n";
$odds = mysql_fetch_array(sql_query("SELECT * FROM betoptions WHERE id =" . sqlesc($id) . "")) or sqlerr(__FILE__, __LINE__);
while ($k = mysql_fetch_array($m)) {
	$body .= "[b]+" . round($k['bonus'] * $odds['odds'] * 0.95, 0) . " 魔力值[/b]，用户 [" . get_username($k['id'], false, false, false, false, false, false, "", false, true) . "] 押注 " . htmlspecialchars($k['bonus']) . " 魔力值 \n";
}
$m = sql_query("SELECT users.username, users.id, bets.userid, bets.bonus FROM bets INNER JOIN users ON bets.userid = users.id WHERE optionid <> $id AND gameid = $gameid ORDER BY bonus DESC LIMIT 20") or sqlerr(__FILE__, __LINE__);
$body.= "\n[b]Top 20 好人：[/b]\n";
while ($k = mysql_fetch_array($m)) {
	$body .= "[b]-" . htmlspecialchars($k['bonus']) . " 魔力值[/b] [" . get_username($k['id'], false, false, false, false, false, false, "", false, true) . "] \n";
}
auto_bet(sqlesc(date("Y-m-d H:i:s", TIMENOW) . ' 博彩结果："' . ($o['heading']) . '"'), sqlesc($body), $subject);
$c = sql_query("SELECT * FROM bets WHERE optionid <> $id AND gameid = " . sqlesc($gameid) . "") or sqlerr(__FILE__, __LINE__);
while ($a = mysql_fetch_array($c)) {
	if (mysql_num_rows(sql_query("SELECT * FROM bettop WHERE userid =" . sqlesc($a["userid"]) . "")) == 0) {
		sql_query("INSERT INTO bettop(userid, bonus, lossbonus, lossnum) VALUES (" . sqlesc($a["userid"]) . ", " . sqlesc($a["bonus"]) . ", " . sqlesc($a["bonus"]) . ",1)") or sqlerr(__FILE__, __LINE__);
	} else {
		sql_query("UPDATE bettop SET lossnum= lossnum + 1, bonus = bonus - " . sqlesc($a["bonus"]) . ", lossbonus = lossbonus + " . sqlesc($a["bonus"]) . " WHERE userid = $a[userid]") or sqlerr(__FILE__, __LINE__);
	}
	$k = mysql_fetch_array(sql_query("SELECT * FROM betgames WHERE id = " . sqlesc($gameid) . "")) or sqlerr(__FILE__, __LINE__);
	$msg2 = <<<EOD
输啦！！！
更不幸的是，你押注在 [i]{$k['heading']}[/i] 上面的魔力值被系统没收啦！
只能祝你下次好运了...

论坛相关帖子：[b][url=forums.php?action=viewtopic&forumid={$forumid}&topicid={$forumlink}]点击这里[/url][/b]
EOD;
	$subjectloss = "博彩输盘";
	sql_query("INSERT INTO messages (sender, receiver, msg, added, subject) VALUES (0, " . sqlesc($a["userid"]) . ", " . sqlesc($msg2) . ", '$date', " . sqlesc($subjectloss) . ")") or sqlerr(__FILE__, __LINE__);
}
sql_query("DELETE FROM betgames WHERE id = " . sqlesc($gameid) . "") or sqlerr(__FILE__, __LINE__);
sql_query("DELETE FROM betoptions WHERE gameid = " . sqlesc($gameid) . "") or sqlerr(__FILE__, __LINE__);
sql_query("DELETE FROM bets WHERE gameid = " . sqlesc($gameid) . "") or sqlerr(__FILE__, __LINE__);
header("location: bet_gamefinish.php");
